import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    delBtnShow: true,
    type: 'add',
    array: [],
    index: null,
    form: {
      id: '',
      materialCode: '',
      uniqueCode: '',
      stepName: '',
      stepCode: '',
      assetNum: '',
      startTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
      endTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
      outPut: '',
      destConsume: '',
      columns: [],
      columnData: []
    }
  },
  onLoad(query) {
    this.data.query = query
    if (query.type == 'add') {
      dd.setNavigationBar({
        title: "新增物料消耗"
      })
      this.setData({
        delBtnShow: false
      })
      this.getMateDataRecordQuery()
    }
    if (query.type == 'edit') {
      dd.setNavigationBar({
        title: "编辑物料消耗"
      })
      this.setData({
        delBtnShow: true
      })
      // 编辑时查询物料详情数据
      this.materialConsumptionQuery(query);
    }
    this.setData({
      type: query.type
    })
  },

  onShow() {
    if (this.data.getSelectTechnics) {
      console.log(this.data.selectTechnicsData)
      this.setData({
        "form.stepName": this.data.selectTechnicsData[0].title,
        "form.stepCode": this.data.selectTechnicsData[0].value
      })
    }
    // 产出物料
    if (this.data.getMaterial) {
      console.log(this.data.materialData)
      this.setData({
        "form.materialCode": this.data.materialData.code,
      })
    }
  },

  // 物料选择
  toMaterialSelection() {
    dd.navigateTo({
      url: '/pages/materialSelection/materialSelection'
    })
  },
  // 消耗开始时间或结束时间选择
  selProData(e) {
    let that = this
    let prop = e.target.dataset.prop;
    let currentDate = e.target.dataset.date
    dd.datePicker({
      format: 'yyyy-MM-dd HH:mm:ss',
      currentDate: currentDate,
      success: (res) => {
        console.log(res)
        if (prop == 'start') {
          that.setData({
            "form.startTime": res.date
          })
        } else {
          that.setData({
            "form.endTime": res.date
          })
        }
      },
    });
  },
  // 工艺步骤跳转
  toSelRange(e) {
    if (this.data.type == "edit") {
      return
    }
    dd.navigateTo({
      url: '/pages/selectTechnics/selectTechnics?type=back'
    })
  },
  // 生产设备弹框处理
  bindObjPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    let ind = e.detail.value
    this.setData({
      "form.assetNum": this.data.array[ind].asset_num,
      "index": ind
    });
  },
  // 关键工艺参数
  toProductKeyParamet() {
    dd.navigateTo({
      url: '/pages/productKeyParamet/productKeyParamet'
    })
  },
  // 保存
  save() {
    let that = this;
    if (that.data.type == 'add') {
      // 新增保存
      $http(apiSetting.materialConsumptionAdd, {
        materialCode: that.data.form.materialCode, //产出物料
        uniqueCode: that.data.form.uniqueCode, //唯一码
        stepCode: that.data.form.stepCode, //工艺步骤
        assetNum: that.data.form.assetNum, //生产设备
        startTime: that.data.form.startTime,  //开始时间
        endTime: that.data.form.endTime,  //结束时间
        outPut: that.data.form.outPut, //数量
        destConsume: that.data.form.destConsume,  //目标唯一码
        columns: that.data.form.columns, //工艺参数
        columnData: that.data.form.columnData  //工艺参数
      }).then((data) => {
        if (data.code == 0) {
          dd.navigateBack({
            delta: 1
          })
        }
      })
    } else {
      // 更新保存
      $http(apiSetting.materialConsumptionUpdate, {
        materialCode: that.data.form.materialCode, //产出物料
        uniqueCode: that.data.form.uniqueCode, //唯一码
        stepCode: that.data.form.stepCode, //工艺步骤
        assetNum: that.data.form.assetNum, //生产设备
        startTime: that.data.form.startTime,  //开始时间
        endTime: that.data.form.endTime,  //结束时间
        outPut: that.data.form.outPut, //数量
        destConsume: that.data.form.destConsume,  //目标唯一码
        columns: that.data.form.columns, //工艺参数
        columnData: that.data.form.columnData,  //工艺参数
        dataId: that.data.form.dataId,
        dapId: that.data.form.dapId,
        id: that.data.form.id, //待更新物料消耗ID  ---------------与新增的区别之处----------------
      }).then((data) => {
        if (data.code == 0) {
          dd.navigateBack({
            delta: 1
          })
        }
      })
    }
  },
  // 删除
  delete() {
    let that = this
    dd.confirm({
      title: '温馨提示',
      content: '删除不可恢复，你确定要删除？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm == true) {
          $http(apiSetting.materialConsumptionDelete, {
            id: that.data.form.id, //物料消耗ID
          }).then((data) => {
            if (data.code == 0) {
              dd.navigateBack({
                delta: 1
              })
            }
          })
        }
      },
    });
  },

  // 查询详情
  materialConsumptionQuery(query) {
    let that = this
    const pages = getCurrentPages();
    let currPage = pages[pages.length - 1]; //当前页面
    let prevPage = pages[pages.length - 2]; //上一个页面
    let list = prevPage.data.list
    let find = list.find(n => {
      return n.id == query.id
    })
    if (find.dapStr) {
      let obj = JSON.parse(find.dapStr)
      let arr1 = []
      let arr2 = []
      for (let i in obj.nodes) {
        if (obj.nodes[i] == 'dap_id_') {
          break
        } else {
          if (obj.data[0][i] != '') {
            arr1.push(obj.data[0][i])
            arr2.push(obj.nodes[i])
          }
        }
      }
      find.columnData = arr1
      find.columns = arr2
    }
    that.setData({
      form: find
    })
    that.getMateDataRecordQuery()
  },

  // 生产设备
  getMateDataRecordQuery() {
    let that = this
    $http(apiSetting.mateDataRecordQuery, {
      metaDataName: "生产设备", //定值：人员 物料 生产设备
    }).then((data) => {
      if (data.code == 0) {
        that.setData({
          array: data.result
        })
        if (that.data.type == 'edit') {
          let fin = data.result.findIndex(n => {
            return n.asset_num == that.data.form.assetNum
          })
          that.setData({
            index: fin
          })
        }
      }
    })
  },

  scanUniqueCode() {
    let that = this
    dd.scan({
      type: 'qr',
      success: (res) => {
        that.setData({
          "form.uniqueCode": res.code
        })
      },
    });
  },
  scanDestConsume() {
    let that = this
    dd.scan({
      type: 'qr',
      success: (res) => {
        that.setData({
          "form.destConsume": res.code
        })
      },
    });
  },

  onInputChange(e) {
    let type = e.target.dataset.type
    let value = e.detail.value
    this.data.form[type] = value
  }


});
